package com.ctg.itrdc.sysmgr.portal.spring.mapper;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Component;

import com.ctg.itrdc.sysmgr.portal.jackson.CustomSerializerFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;

@Component("ctgObjectMapper") 
public class CtgObjectMapper extends ObjectMapper {

	private static final long serialVersionUID = -8122268867798140209L;

	public CtgObjectMapper() {  
        CustomSerializerFactory factory = new CustomSerializerFactory();  
        factory.addGenericMapping(Date.class, new JsonSerializer<Date>() {  
            @Override  
            public void serialize(Date value, JsonGenerator jsonGenerator,  
                    SerializerProvider provider) throws IOException, JsonProcessingException {  
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
                jsonGenerator.writeString(sdf.format(value));  
            }  
        });  
        this.setSerializerFactory(factory);  
    } 
}
